'use strict';

const conn = require('../../libs/DB/Conn');
const utils = require('../../libs/Utils');
module.exports = async function (params) {
    if (params.agentName) {
        let checkUserSql = `SELECT
                          count(*) num
                           FROM
                            user_file uf                         
                           WHERE login_name = ? AND delete_sign = 1`;
        let queryResult = await conn(checkUserSql, [params.loginName]);
        if (queryResult[0].num > 0) {
            return {success: false, message: '登录名已存在'}
        } else {
            let insertAgentObj = {
                agent_name: params.agentName,
                agent_principal: params.agentPrincipal,
                phone_number: params.phoneNumber,
                company_addr: params.companyAddr
            };
            if (params.pAgentId) insertAgentObj['p_id'] = params.pAgentId;
            let insertResult = await conn(`INSERT INTO agent_file SET ?`, [insertAgentObj]);
            if (insertResult.insertId) {
                let insertObj = {
                    login_name: params.loginName,
                    password: utils.encrypt("123456"),
                    user_name: params.userName,
                    agent_id: insertResult.insertId,
                    group_id: 3,
                    mobile: params.mobile,
                };
                let insertSql = `INSERT INTO user_file SET ?`;
                await conn(insertSql, [insertObj]);
                return {success: true, message: '添加成功'}
            }else{
                return {success: false, message: '添加代理商失败'};
            }

        }

        return {success: true}

    } else {
        return {success: false}
    }

};